package org.jash.shop;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.google.android.material.appbar.MaterialToolbar;

import org.jash.shop.entity.AddToCartEntity;
import org.jash.shop.entity.GoodsEntity;

import java.util.HashMap;

public class DetailActivity extends AppCompatActivity {
    private TextView title, price;
    private ImageView icon, imgOne, imgTwo;
    private MaterialToolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        toolbar = findViewById(R.id.toolbar);
        title = findViewById(R.id.title);
        price = findViewById(R.id.price);
        icon = findViewById(R.id.icon);
        imgOne = findViewById(R.id.img_one);
        imgTwo = findViewById(R.id.img_two);
        GoodsEntity.DataDTO dto = (GoodsEntity.DataDTO) getIntent().getSerializableExtra("goods");
        title.setText(dto.getGoodsDesc());
        price.setText("￥" + dto.getGoodsPrice());
        Glide.with(icon).load(dto.getGoodsIcon()).into(icon);
        Glide.with(imgOne).load(dto.getGoodsDetailOne()).into(imgOne);
        Glide.with(imgTwo).load(dto.getGoodsDetailTwo()).into(imgTwo);
        toolbar.setNavigationOnClickListener(v -> finish());
        findViewById(R.id.add_cart).setOnClickListener(v -> {
            HttpUtils.doGet("http://10.161.9.80:7003/car/addCar", new HashMap<String, Object>(){{
                put("goods_id", dto.getId());
                put("username", "1");
            }}, AddToCartEntity.class,
                    r -> {
                        if (r.getErrorCode() == 200) {
                            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(this, r.getErrorMessage(), Toast.LENGTH_SHORT).show();
                        }
                    },
                    t -> {
                        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
                    });
        });

    }
}